<?php

/**
 *  52zll.top
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

require DISCUZ_ROOT.'./source/plugin/dzapp_zhibo/class/zhiboController.php';
$ops = array('list', 'post', 'delete', 'order');
$op = !in_array($_GET['op'], $ops) ? 'list' : $_GET['op'];

$perpage = 20;
$page = max(1, intval($_GET['page']));
$start = ($page - 1) * $perpage;
if($start < 0) $start = 0;

if($op == 'list') {

	if(!submitcheck('listsubmit')) {
		
		$param = array(
			array('name', $_GET['name'], 'like', '%', '%')
		);
	
		$count = C::t('#dzapp_zhibo#dzapp_zhibo_gift')->count_by_search($param);
		if($count) {
			$query = C::t('#dzapp_zhibo#dzapp_zhibo_gift')->fetch_all_by_search($param, $start, $perpage);
			foreach($query as $value) {
				$value['icon'] = $value['icon'] ? dzapp_pic_get($value['icon'], 'portal', 0, $_G['setting']['ftp']['on']) : 'static/image/common/nophoto.gif';
				$list[] = $value;
			}
			unset($_GET['page']);
			$mpurl = 'plugin.php?'.page_url($_GET);
			$multipage = multi($count, $perpage, $page, $mpurl);
		}
		
	} else {

		if(is_array($_GET['delete'])) {
			C::t('#dzapp_zhibo#dzapp_zhibo_gift')->delete($_GET['delete']);
		}
		
		showmessage('operation_done', 'plugin.php?id=dzapp_zhibo&mod=zhibocp&ac=gift');
	}

} elseif ($op == 'post') {

	if($_GET['gift_id']){
		
		$gift = C::t('#dzapp_zhibo#dzapp_zhibo_gift')->fetch($_GET['gift_id']);
		if(empty($gift)) {
			showmessage('dzapp_zhibo:required_validate');
		}
		
		$_GET['icon'] = $gift['icon'];
		$gift['icon'] = $gift['icon'] ? dzapp_pic_get($gift['icon'], 'portal', 0, $_G['setting']['ftp']['on']) : 'static/image/common/nophoto.gif';
		
	}
	
	if(submitcheck('giftsubmit')) {
		
		if(!$_GET['name']) {
			showmessage('dzapp_zhibo:required_validate');
		}
		
		if($icon = dzapp_pic_upload($_FILES['icon'], 'portal', 320, 240)) $_GET['icon'] = $icon['pic'];
		if($icon && $gift['icon']) dzapp_pic_delete($gift['icon'], 'portal', 1);
		
		$data = array(
			'name'              => $_GET['name'],
			'icon'        => $_GET['icon'],
			'price'          => $_GET['price'],
		);
		
		if($gift['gift_id']){
			C::t('#dzapp_zhibo#dzapp_zhibo_gift')->update($gift['gift_id'], $data);
		}else{
			
			$data['dateline'] = $_G['timestamp'];
			$data['ip'] = $_G['ip'];
			
			C::t('#dzapp_zhibo#dzapp_zhibo_gift')->insert($data);
			
		}
		showmessage('operation_done', 'plugin.php?id=dzapp_zhibo&mod=zhibocp&ac=gift');
		
	}
} elseif ($op == 'order') {

	$param = array(
		array('username', $_GET['username'], 'like', '%', '%'),
		array('activity_id', $_GET['activity_id']),
	);

	$count = C::t('#dzapp_zhibo#dzapp_zhibo_order')->count_by_search($param);
	if($count) {
		$query = C::t('#dzapp_zhibo#dzapp_zhibo_order')->fetch_all_by_search($param, $start, $perpage);
		foreach($query as $value) {
			$activity_id[] = $value['activity_id'];
			$gift_id[] = $value['gift_id'];
			$value['dateline'] = gmdate("Y-m-d H:i:s", $value['dateline']);
			$list[] = $value;
		}
		unset($_GET['page']);
		$mpurl = 'plugin.php?'.page_url($_GET);
		$multipage = multi($count, $perpage, $page, $mpurl);
	}
	$activity = C::t('#dzapp_zhibo#dzapp_zhibo_activity')->fetch_all($activity_id);
	$gift = C::t('#dzapp_zhibo#dzapp_zhibo_gift')->fetch_all($gift_id);
}

include_once template("dzapp_zhibo:zhibocp_gift");

?>